/*********************************************************************************
 *      Copyright:  (C) 2022 Li Yanting<liyanting2@163.com>
 *                  All rights reserved.
 *
 *       Filename:  convert.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(05/10/2022)
 *         Author:  Li Yanting <liyanting2@163.com>
 *      ChangeLog:  1, Release initial version on "05/10/2022 16:38:58"
 *                 
 ********************************************************************************/
#ifndef _PDU_H_
#define _PDU_H_

#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <iconv.h>
#include "logger.h"

#define MAX_UNIC_LEN 512

/*********************************************************************************
 *      Function name:  proc_ctr_num
 *        Description:  handle sms center number; remove character "+" -> add character F at the tail 
 *                       -> swap odd and even bits -> add head "0891"
 *         @parameter:  ctr_num  sms center number "+86xxxxxxxxxxx" 
 *         @parameter:  ctr_num_buf --a buf for handled sms center number, at least size 32 !!!
 *         @parameter:  buf_size   
 *       Return value:  On error, -1 is returned. On success, 0 is returned.
 ********************************************************************************/
int proc_ctr_num(char *ctr_num, char *ctr_num_buf, int buf_size);


/*********************************************************************************
 *      Function name:  proc_num
 *        Description:  handle phone number; remove character "+" -> add character F at the tail
 *                      -> swap odd and even bits -> add head "11000D91" and tail "000800"
 *         @parameter:  num  phone number "+86xxxxxxxxxxx"
 *         @parameter:  num_buf  a buf for handled phone number, at least size 32 !!!
 *         @parameter:  buf_size   
 *       Return value:  On error, -1 is returned. On success, 0 is returned.
 ********************************************************************************/
int proc_num(char *num, char *num_buf, int buf_size);


/*********************************************************************************
 *      Function name:  utf8_to_unic
 *        Description:  utf8 to unicode
 *         @parameter:  utf8  utf8 sms
 *         @parameter:  unic_buf a buf for unic, decide send the most string len, recommend buf size 512 !!!
 *         @parameter:  unic_len --return unic len !!!
 *       Return value:  On error, -1 is returned. On success, 0 is returned.
 ********************************************************************************/
int utf8_to_unic(char *utf8, char *unic_buf, int *unic_len);


/*********************************************************************************
 *      Function name:  hex_to_str
 *        Description:  byte flow to character flow
 *         @parameter:  str_buf  a buf for str message, at least buf size is twice the size of hex message !!!
 *         @parameter:  hex  hex message
 *         @parameter:  hex_len  hex message len
 *       Return value:  On error, -1 is returned. On success, 0 is returned.
 ********************************************************************************/
int hex_to_str(const char *hex, int hex_len, char *str_buf);



/*********************************************************************************
 *      Function name:  str_to_hex
 *        Description:  character flow to byte flow
 *         @parameter:  hex_buf  a buf for hex message, at least buf size is 1/2 the size of str message !!!
 *         @parameter:  str  str message
 *       Return value:  On error, -1 is returned. On success, 0 is returned.
 ********************************************************************************/
int str_to_hex(const char *str,  char *hex_buf);



/*********************************************************************************
 *      Function name:  pdu_encode
 *        Description:  convert sms to pdu, output max pdu len is 512
 *         @parameter:  utf8  utf8 sms
 *         @parameter:  ctr_num  "+86xxxxxxxxxxx"
 *         @parameter:  num  "+86xxxxxxxxxxx"
 *         @parameter:  pdu_buf  recieve pdu sms buf, at least buf size 1090 !!!
 *         @parameter:  buf_size  buf size
 *         @parameter:  msg_len  len/2 of "phone_number + str_unicode_length/2 + str_unicode"
 *       Return value:  On error, -1 is returned. On success, 0 is returned.
 ********************************************************************************/
int pdu_encode(char *utf8, char *ctr_num, char *num, char *pdu_buf, int buf_size, int *msg_len);


/*********************************************************************************
 *      Function name:  tolower
 *        Description:  Capital letter to lowercase
 *         @parameter:  c  char
 *       Return value:  on success, lowercase letters.
 ********************************************************************************/
int tolower(int c);


/*********************************************************************************
 *      Function name:  htoi
 *        Description:  hex to int
 *         @parameter:  c  hex
 *       Return value:  on success, int
 ********************************************************************************/
int htoi(char *s);

/*********************************************************************************
 *      Function name:  bit7_to_unic
 *        Description:  bit-7 to unicode
 *         @parameter:  bit7  bit 7
 *         @parameter:  unic_buf  a buf fot unicode
 *         @parameter:  unic_len  unicode len
 *       Return value:  on success, 0; on error, < 0
 ********************************************************************************/
int bit7_to_unic(char *bit7, char *unic_buf, int *unic_len);


/*********************************************************************************
 *      Function name:  pdu_decode
 *        Description:  pdu_decode
 *         @parameter:  recv  
 *         @parameter:  num_buf  
 *         @parameter:  ctr_num  
 *         @parameter:  time_buf  
 *         @parameter:  utf8_buf  
 *       Return value:  on success, 0; on error, < 0
 ********************************************************************************/
int pdu_decode(char *recv, char *num_buf, char *ctr_num, char *time_buf, char *utf8_buf);

#endif